<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2017/11/9  19:27
 */
namespace app\api\logic;
use app\api\BaseModel;

class Cart extends  BaseModel{

	//检测参数是否已传
    protected $ParameterDetection  = [
    	'cartAdd'=>['cid','goodsid','total'],
    	'cartDel'=>['cid','id'],
    	'cartUpdate'=>['cid','goodsid','total','edit'],
    ];

	/**
	* 购物车列表
	*/
	public function cartList(){
		$result = [];
		$cart_result = db('cart')->where(['user_id'=>$this->user['id']])->order('cid desc')->select();
		if(!empty($cart_result)){
			$circle_name = array_column($cart_result, 'circle_name');
			$circle_name = array_values(array_unique($circle_name));
			foreach ($circle_name as $k => $v) {
				$result[$k]['circle_name'] = $v;
				foreach ($cart_result as $kk => &$vv) {
					if($vv['circle_name'] == $v){
						//查询产品库存
						$goods_stock = db('circle_goods')->where(['id'=>$vv['goodsid']])->field('stock')->find();

						$vv['goods_stock'] = $goods_stock['stock'];
						$vv['goods_price'] = $vv['goods_price']/100;
						$vv['goods_image'] = IMG_PATH.$vv['goods_image'];
						$vv['goods_test'] = 'false';
						$result[$k]['cart'][] = $vv;
					}
				}
			}
		}
		return 	$result;	
	}

    /**
     * 购物车添加
     */
    public function cartAdd(){
		$cid = $this->requestData['cid'];
		$goodsid = $this->requestData['goodsid'];
		$total = $this->requestData['total'];
		
		if(empty($total))return ['message'=>'请填写购买数量!'];
		$circle_goods_result = db('circle_goods')->where(['id'=>$goodsid,'status'=>'1'])->find();
		if(empty($circle_goods_result))return ['message'=>'商品不存在或者已下架'];
		$circle_result = db('circle')->where(['id'=>$cid])->find();
		if(empty($circle_result))return ['message'=>'参数非法!'];
		if($circle_goods_result['cid'] != $cid)return ['message'=>'cid参数错误!'];

		//查询该商品是否已
		$cart_result = db('cart')->where(['cid'=>$cid,'goodsid'=>$goodsid,'user_id'=>$this->user['id']])->find();

		if(empty($cart_result)){
			$data['cid'] = $cid;
			$data['circle_name'] = $circle_result['circle_name'];
			$data['user_id'] = $this->user['id'];
			$data['goodsid'] = $goodsid;
			$data['goods_price'] = $circle_goods_result['price'];
			$data['goods_name'] = $circle_goods_result['name'];
			$data['goods_image'] = isset($circle_goods_result['image'])?$circle_goods_result['image']:'';
			$data['total'] = $total;
			$data['createtime'] = time();

			db('cart')->insert($data);
		}else{
			db('cart')->where(['cid'=>$cid,'goodsid'=>$goodsid,'user_id'=>$this->user['id']])->setInc('total',$total);
		}
    }

    /**
    *  购物车删除
    */
    public function cartDel(){
    	$cid = $this->requestData['cid'];
    	$id = $this->requestData['id'];

    	if(!empty($cid) && !empty($id))return ['message'=>'参数不能同时传'];

    	$result = '';
    	if(!empty($cid)){
    		$result = db('cart')->where(['cid'=>$cid])->delete();
    	}

    	if(!empty($id)){
    		$result = db('cart')->where(['id'=>$id])->delete();
    	}

    	if(!$result)return ['message'=>'删除失败!'];
    }

    /**
    *  购物车更新
    */
    public function cartUpdate(){
    	$cid = $this->requestData['cid'];
		$goodsid = $this->requestData['goodsid'];
		$total = $this->requestData['total'];
		$edit = $this->requestData['edit'];

		$total = intval($total);
		if(empty($total))return ['message'=>'total不能为空!'];

		$result = '';

		if($edit == 'add'){
			$result = db('cart')->where(['cid'=>$cid,'goodsid'=>$goodsid,'user_id'=>$this->user['id']])->setInc('total',$total);
		}

		if($edit == 'cut'){
			//查询该商品是否已
			$cart_result = db('cart')->where(['cid'=>$cid,'goodsid'=>$goodsid,'user_id'=>$this->user['id']])->find();

			if($cart_result['total'] < $total)return ['message'=>'数量过大!'];

			$result = db('cart')->where(['cid'=>$cid,'goodsid'=>$goodsid,'user_id'=>$this->user['id']])->setDec('total',$total);
		}

		if(!$result)return ['message'=>'编辑失败!'];
    }
}